﻿/// <reference path="../Policy/apiSvc.js" />
/// <reference path="../Policy/editCtrl.js" />
/// <reference path="../Policy/listCtrl.js" />

/// <reference path="sharedSetup.js" />

describe("policy", function () {
    "use strict";
    runSharedSetup();
    
    var $httpBackend;   
    beforeEach(inject(function (_$httpBackend_) {
        $httpBackend = _$httpBackend_;       
    }));

    beforeEach(inject(function (_$httpBackend_) {
        $httpBackend.whenGET('/api/static').respond({ "Value": { "Dictionaries": [{ "Id": 1, "DictionaryID": 0, "Name": "Pending", "DictionaryName": "States" }, { "Id": 2, "DictionaryID": 0, "Name": "Approved", "DictionaryName": "States" }, { "Id": 3, "DictionaryID": 0, "Name": "Rejected", "DictionaryName": "States" }, { "Id": 1, "DictionaryID": 0, "Name": "Approve", "DictionaryName": "Actions" }, { "Id": 2, "DictionaryID": 0, "Name": "Reject", "DictionaryName": "Actions" }, { "Id": 1, "DictionaryID": 1, "Name": "Gasoline", "DictionaryName": "TypesOfFuels" }, { "Id": 4, "DictionaryID": 1, "Name": "Diesel", "DictionaryName": "TypesOfFuels" }], "LocaleStrings": [{ "Id": 35, "Name": "Messages.Success", "Value": "Success", "CultureID": 1, "CultureCode": "en-GB" }, { "Id": 36, "Name": "Messages.Success", "Value": "Succes", "CultureID": 2, "CultureCode": "ro-RO" }], "Settings": { "PageSize": 10, "LinksSize": 5 }, "CurrentCulture": "ro-RO" }, "ShowNotification": false, "Success": true, "ErrorMessage": null, "BrokenRules": [] });        
    }));
    
    describe("listCtrl", function () {
        var scope, rootScope;
        var $timeout;

        beforeEach(inject(function ($rootScope, $controller, $injector, $timeout) {
            scope = $rootScope.$new();            
            rootScope = $rootScope;
            scope.initialListResponse = { "PageNumber": 1, "PageSize": 5, "TotalItemCount": 2, "List": [{ "PlateNo": "122", "Id": 1, "Number": 1223, "PersonID": 2, "Person": { "Id": 2, "Name": "12" } }, { "PlateNo": "11123", "Id": 26, "Number": 102, "PersonID": 3, "Person": { "Id": 3, "Name": "2" } }], "PageCount": 1, "Success": true, "ErrorMessage": null, "BrokenRules": [] };
                                       
            $controller("listCtrl", {
                $scope: scope
            });            
            $timeout.flush();            
        }));
        
        describe("on Render", function () {                            
            it("should Init the list", function () {                    
                expect(scope.list.length).toBe(2);
            });                
        });
        
        describe("on Delete", function () {
            beforeEach(function () {                
                var id = scope.list[1].Id;
                scope.remove(1);

                $httpBackend.whenDELETE('/api/policies/26').respond({ "Success": true, "ErrorMessage": null, "BrokenRules": [] });
                $httpBackend.expect("DELETE", "/api/policies/" + id);
                $httpBackend.flush();
            });
            it("should Call a DELETE /api/policies/", function () {
               
            });
            it("should Remove item from list", function () {
                expect(scope.list.length).toBe(1);
            });
        });

        describe("on Click", function () {
            beforeEach(function () {                
                scope.edit(0);
            });
            it("should Set the index", function () {
                expect(scope.selectedIndex).toBe(0);
            });
        });

        describe("on Search", function () {
            beforeEach(function () {
                scope.searchForm = { $valid: true };
                scope.search({ Number: 1223 });
                
                $httpBackend.whenPOST('/api/policies/searches', { "PageNumber": 1, "PageSize": 10, "Count": true, "SortColumn": "Id", "SortAscending": true, "Number": 1223 }).respond({ "PageNumber": 1, "PageSize": 5, "TotalItemCount": 1, "List": [{ "PlateNo": "122", "Id": 1, "Number": 1223, "PersonID": 2, "Person": { "Id": 2, "Name": "12" } }], "PageCount": 1, "Success": true, "ErrorMessage": null, "BrokenRules": [] });
                $httpBackend.expect("POST", "/api/policies/searches", { "PageNumber": 1, "PageSize": 10, "Count": true, "SortColumn": "Id", "SortAscending": true, "Number": 1223 });
                $httpBackend.flush();
            });
            it("should Call a POST /api/policies/searches", function () {
                
            });
            it("should Set the list", function () {
                expect(scope.list.length).toBe(1);
            });
        });

        describe("on itemAdded", function () {
            beforeEach(function () {
                var childScope = scope.$new();
                childScope.$emit('itemAdded', { Id: 2, Number: 2222 });
            });

            it("should Add item to list", function () {
                expect(scope.list.length).toBe(3);
            });
        });

        describe("on itemEdited", function () {
            beforeEach(function () {                
                scope.edit(0);
                var item = scope.list[scope.selectedIndex];
                item.Number = 4444;

                var childScope = scope.$new();
                childScope.$emit('itemEdited', item);
            });

            it("should Set the list", function () {
                expect(scope.list[0].Number).toBe(4444);
            });
        });                 
    });    
});